﻿@using PagedList.Mvc
@model PagedList.IPagedList<DataAccess.DB.XuatKho>
@using DuocThaiBinh.WebUI.HtmlHelpers;
@{
    ViewBag.Title = "Xuất kho";
}
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@using (Html.BeginForm("Index", "XuatKho", FormMethod.Get))
{   
    <fieldset>
        <legend>Tìm kiếm</legend>

        <table>
            <tr>
                <td style="width: 20%">
                    <span>Mã khách hàng:</span>
                </td>
                <td>
                    @Html.TextBox("txtSearch", ViewBag.CurrentFilter as string)
                    <input id="popupKhachHang" type="button" value="+" style="width: 30px" />
                </td>
                <td>
                    <span>Mã sản phẩm :</span>
                </td>
                <td>
                    @Html.TextBox("SearchMaSP")
                    <input id="popupSanPham" type="button" value="+" style="width: 30px" onclick="showattach()" />
                </td>
            </tr>
            <tr>
                <td>
                    <span>Tên khách hàng :</span>
                </td>
                <td>
                    @Html.TextBox("txtTenKH", null, new { @disabled = "disabled" })
                </td>
                <td>
                    <span>Tên sản phẩm :</span>
                </td>
                <td>
                    @Html.TextBox("SearchTenSP", null, new { @disabled = "disabled" })
                </td>
            </tr>
            <tr>
                <td>
                    <span>Địa chỉ</span>
                </td>
                <td>
                    @Html.TextArea("txtDiaChi", null, new { @disabled = "disabled" })
                </td>
                <td>
                    <span>Mã hóa đơn :</span>
                </td>
                <td>
                    @Html.TextBox("txtMHD")
                </td>
            </tr>
            <tr>
                <td>
                    <span>Nhân viên giao dịch: </span>
                </td>
                <td>
                    @Html.DropDownList("ID_NguoiXuatKho", String.Empty)
                </td>
            </tr>
            <tr>
                <td>Ngày xuất kho:
                </td>
                <td>
                    <span>Từ ngày :</span>@Html.TextBox("dtFrom", ViewBag.crFr as string, new { @style = "width:100px" })
                    <span>Đến ngày : </span>
                    @Html.TextBox("dtTo", ViewBag.crTo as string, new { @style = "width:100px" })
                </td>
            </tr>

            <tr>
                <td colspan="2" style="text-align: center">
                    <input value="Tìm kiếm" type="submit" style="padding: 7px" name="btnCTT" />
                    <input value="In thống kê hóa đơn" type="button" style="padding: 7px" id="btnInTK" onclick="RedirectPrintf();" />
                    <input value="In thống kê sản phẩm" type="button" style="padding: 7px" id="btnInSP" onclick="RedirectPrintfSP();" />
                </td>
            </tr>
        </table>
    </fieldset>
}
<p style="padding: 0 0 20px 0">
    @Html.ActionLink("Thêm mới hóa đơn xuất kho", "Create", null, new { @class = "linkCreate" })
</p>
@{var currentRole = Roles.GetRolesForUser(User.Identity.Name);}
<table class="ms-list-form">
    <tr>
        <th>
            <span>Mã HĐ</span>
        </th>

        <th>
            <span>Khách hàng</span>
        </th>
        <th>
            <span>Địa chỉ KH</span>
        </th>
        <th>
            <span>NV giao dịch</span>
        </th>
        <th>
            <span>Ngày xuất kho</span>
        </th>
        <th>
            <span>Tổng thanh toán</span>
        </th>
        @{if (currentRole.Contains("Administrator"))
          {
            <th><span>Lãi</span></th>
          }}

        <th></th>
    </tr>


    @foreach (var item in Model)
    {
        <tr>
            <td style="border-left: 1px solid #d4d4d4">
                @Html.DisplayFor(modelItem => item.MaHoaDon)
            </td>

            <td>
                @Html.DisplayFor(modelItem => item.KhachHang.MaKH)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.KhachHang.DiaChiKH)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UserProfile.Code)
            </td>

            <td style="text-align: right">
                <span>@DuocThaiBinh.Common.Util.ConvertObjectToStringDateTime(item.NgayXuatKho)</span>
            </td>
            <td style="text-align: right">
                @Html.DisplayFor(modelItem => item.TongThanhToan)
            </td>
            @{if (currentRole.Contains("Administrator"))
              {
                <td style="text-align: right">@Html.DisplayFor(modelItem => item.Lai)</td>
              }}

            <td style="text-align: center; border-right: 1px solid #d4d4d4; width: 220px">
                @Html.ActionLink("Sửa", "Edit", new { id = item.ID }, new { @class = "btnEdit" })
                @Html.ActionLink("In", "Details", new { id = item.ID }, new { @class = "btnEdit" })
                @Html.ActionLink("Thanh toán", "ThanhToan", new { id = item.ID }, new { onclick = "return confirm('Bạn có muốn thanh toán hóa đơn " + @item.MaHoaDon + "')", @class = "btnEdit" })
                @Html.ActionLink("Xóa", "Delete", new { id = item.ID }, new { onclick = "return confirm('Bạn có muốn xóa hóa đơn " + @item.MaHoaDon + "')", @class = "btnEdit" })
            </td>
        </tr>
    }

</table>
<div id="dialog-KH" style="display: none" title="Danh sách khách hàng">
</div>
<div id="dialog-modal" style="display: none" title="Danh sách sản phẩm">
    
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $(".linkCreate").focus();
        $("#dtFrom").datepicker();
        $("#dtTo").datepicker();

    });

    $("#popupKhachHang").click(function () {
        $("#dialog-KH").dialog({
            width: 1000,
            height: 600,
            modal: true,

            open: function () {
                $(this).load("/KhachHang/IndexPopup", function () {

                    $("#txtMaKH").focus();
                    $(document).on('input', '#txtMaKH', function () {
                        var valueSearch = $(this).val();
                        $.ajax({
                            url: '@Url.Action("GetTableKhachHangById", "KhachHang")',
                            type: 'POST',
                            data: { "valueSearch": valueSearch },
                            dataType: 'json',
                            success: function (response) {
                                var productDetails = response.ProductDetail;
                                $("#tablekh tbody").html(productDetails);
                            }
                        });

                    });
                    $(".btnChon").click(function () {

                        var idKH = $(this).attr('id');
                        $.ajax({
                            url: '@Url.Action("GetKhachHangById", "KhachHang")',
                            type: 'POST',
                            data: { "valueSearch": idKH },
                            dataType: 'json',
                            success: function (response) {
                                var KhachHangDetail = response.KhachHangDetail;
                                var listKhachHangInfo = KhachHangDetail.split(";#");
                                $("#txtSearch").val(listKhachHangInfo[1]);
                                $("#txtTenKH").val(listKhachHangInfo[2]);
                                $("#txtDiaChi").val(listKhachHangInfo[3]);
                                $("#dialog-KH").dialog("close");
                            }
                        });
                    });
                });

            },

        });
        $("#dialog-KH").dialog("open");
    });

    function showattach(val) {
        $("#rowsp").val(val);
        $("#dialog-modal").dialog({
            width: 1000,
            height: 600,
            modal: true,
        });
    };
    $(document).on('input', '#txtSearchMSP', function () {

        var valueSearch = $(this).val();
        $.ajax({
            url: '@Url.Action("GetSPXuatKhoById", "SanPham")',
            type: 'POST',
            data: { "valueSearch": valueSearch },
            dataType: 'json',
            success: function (response) {
                var productDetails = response.ProductDetail;
                $("#tablesp tbody").html(productDetails);
            }
        });
    });


        function SelectSP(sp) {
            var row = $("#rowsp").val();

            var selectedValue = sp;

            $.ajax({
                url: '@Url.Action("GetProductXKById", "SanPham")',
                type: 'POST',
                data: { "selectedValue": selectedValue },
                dataType: 'json',
                success: function (response) {
                    var productDetails = response.ProductDetail;

                    var productdetail = productDetails.split(';#');

                    $("#SearchMaSP").val(productdetail[0]);
                    $("#SearchTenSP").val(productdetail[3]);

                }
            });
            $("#dialog-modal").dialog("close");

        }

        function SelectKH(idKH) {

            $.ajax({
                url: '@Url.Action("GetKhachHangById", "KhachHang")',
                type: 'POST',
                data: { "valueSearch": idKH },
                dataType: 'json',
                success: function (response) {
                    var KhachHangDetail = response.KhachHangDetail;
                    var listKhachHangInfo = KhachHangDetail.split(";#");
                    $("#txtSearch").val(listKhachHangInfo[1]);
                    $("#txtTenKH").val(listKhachHangInfo[2]);
                    $("#txtDiaChi").val(listKhachHangInfo[3]);
                    $("#dialog-KH").dialog("close");
                }
            });
        }
        function RedirectPrintf() {
            var txtSearch = $("#txtSearch").val();
            var dtFrom = $("#dtFrom").val();
            var dtTo = $("#dtTo").val();
            var nvgd = $("#ID_NguoiXuatKho").val();
            var msp = $("#SearchMaSP").val();
            var url = window.location.href.split('?')[0] + "/IndexThongKe/?";
            if (txtSearch != '') {
                url = url + "&khachhang=" + txtSearch
            }
            if (dtFrom != '') {
                url = url + "&from=" + dtFrom
            }
            if (dtFrom != '') {
                url = url + "&to=" + dtTo
            }
            if (nvgd != "") {
                url = url + "&nv=" + nvgd;
            }
            if (msp != "") {
                url = url + "&masp=" + msp;
            }
            window.open(url);
        }
        function RedirectPrintfSP() {
            var txtSearch = $("#txtSearch").val();
            var dtFrom = $("#dtFrom").val();
            var dtTo = $("#dtTo").val();
            var nvgd = $("#ID_NguoiXuatKho").val();

            var url = window.location.href.split('?')[0] + "/IndexThongKeSanPham/?";
            if (txtSearch != '') {
                url = url + "&khachhang=" + txtSearch
            }
            if (dtFrom != '') {
                url = url + "&from=" + dtFrom
            }
            if (dtFrom != '') {
                url = url + "&to=" + dtTo
            }
            if (nvgd != "") {
                url = url + "&nv=" + nvgd;
            }

            window.open(url);
        }
        $(':input').mouseover(function () {
            $(this).attr("title", $(this).val());
        });

</script>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, currentFilter = ViewBag.CurrentFilter, crFr = ViewBag.crFr, crTo = ViewBag.crTo }))